/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved. 
 */
package com.icinfo.cloud.provider.punish.ucase.casemain.service.impl;

import com.icinfo.cloud.common.security.util.UserUtil;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.common.utils.SMSUtils;
import com.icinfo.cloud.provider.punish.config.utils.AuditDataUtil;
import com.icinfo.cloud.provider.punish.ucase.casemain.dto.AppendixDto;
import com.icinfo.cloud.provider.punish.ucase.casemain.dto.CaseIllegalParkingRecordDto;
import com.icinfo.cloud.provider.punish.ucase.casemain.dto.CaseIllegalParkingRecordRequest;
import com.icinfo.cloud.provider.punish.ucase.casemain.mapper.CaseAppendixRecordMapper;
import com.icinfo.cloud.provider.punish.ucase.casemain.mapper.CaseIllegalParkingRecordMapper;
import com.icinfo.cloud.provider.punish.ucase.casemain.model.CaseAppendixRecord;
import com.icinfo.cloud.provider.punish.ucase.casemain.model.CaseIllegalParkingRecord;
import com.icinfo.cloud.provider.punish.ucase.casemain.service.ICaseIllegalParkingRecordService;
import com.icinfo.cloud.provider.punish.ucase.casemain.vo.CaseIllegalParkingRecordVo;
import com.icinfo.cloud.xzcf.provider.member.userinfo.dto.UserDTO;
import com.icinfo.framework.core.service.support.MyBatisServiceSupport;
import com.icinfo.framework.mybatis.mapper.entity.Example;
import com.icinfo.framework.mybatis.pagehelper.PageHelper;
import com.icinfo.framework.tools.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 描述: 机动车违停记录表 case_illegal_parking_record 对应的Service接口实现类.<br>
 *
 * @author framework generator
 * @date 2022年12月08日
 */
@Service
public class CaseIllegalParkingRecordServiceImpl extends MyBatisServiceSupport implements ICaseIllegalParkingRecordService {
    /**
     * 日志记录器
     */
    private static final Logger logger = LoggerFactory.getLogger(CaseIllegalParkingRecordServiceImpl.class);

    @Autowired
    private CaseIllegalParkingRecordMapper caseIllegalParkingRecordMapper;

    @Autowired
    private CaseAppendixRecordMapper caseAppendixRecordMapper;

    /**
     * 获取机动车违停记录列表
     * @param request
     * @return
     */
    @Override
    public List<CaseIllegalParkingRecordVo> getIllegalParkingList(CaseIllegalParkingRecordRequest request) {
        request.setOrgCode(UserUtil.getUserInfo().getOrgCode());
        PageHelper.startPage(request.getPageNum(), request.getLength(), request.getOrderBy());
        List<CaseIllegalParkingRecordVo> illegalParkingList = caseIllegalParkingRecordMapper.getIllegalParkingList(request);
        return illegalParkingList;
    }

    /**
     * 新增机动车违停记录
     * @param dto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer saveIllegalParkingRecord(CaseIllegalParkingRecordDto dto) {
        String uuid = StringUtils.uuid();
        CaseIllegalParkingRecord caseIllegalParkingRecord = new CaseIllegalParkingRecord();
        BeanUtils.copyProperties(dto, caseIllegalParkingRecord);
        caseIllegalParkingRecord.setOrgCode(UserUtil.getUserInfo().getOrgCode());
        caseIllegalParkingRecord.setOrgName(UserUtil.getUserInfo().getOrgName());
        caseIllegalParkingRecord.setId(uuid);
        AuditDataUtil.fillAuditAddData(caseIllegalParkingRecord);
        int i = caseIllegalParkingRecordMapper.insertSelective(caseIllegalParkingRecord);
        dto.setId(uuid);
        // 保存附件
        saveAppendixList(dto);
        // 发送短信
        if (Constant.STRING_1.equals(dto.getIsSend())){
            String messageContent= "【" + UserUtil.getUserInfo().getOrgName() + "】" + dto.getCarNumber() + "车主，执法人员检查发现你的车辆在"+ dto.getIllegalHappenAddress() + dto.getIllegalHappenAddressDetail()+"存在在人行道违法停放机动车的违法行为，请立即到场整改。经责令改正拒不改正的，将依据《中华人民共和国行政强制法》第五十二条之规定，将对车辆强制拖离，所需费用由你本人承担。";
            try {
                SMSUtils.sendTextMessage(dto.getPhone(),messageContent,"");
            } catch (Exception e) {
                logger.error(e.getMessage());
            }
        }
        return i;
    }

    /**
     * 附件上传
     * @param dto
     * @return
     */
    private String saveAppendixList(CaseIllegalParkingRecordDto dto) {
        UserDTO userDTO = UserUtil.getUserInfo();
        StringBuffer attachmentId=new StringBuffer("");
        List<AppendixDto> appendixDtoList=dto.getAppendixList();
        if(!ObjectUtils.isEmpty(appendixDtoList)){
            for (AppendixDto appendixDto:appendixDtoList) {
                String id = StringUtils.uuid();
                CaseAppendixRecord caseAppendixRecord = new CaseAppendixRecord();
                caseAppendixRecord.setCaseId(dto.getId());
                caseAppendixRecord.setCaseStageCode("");
                caseAppendixRecord.setAppendixTypeId("ILLEGAL_PARKING_RECORD");
                caseAppendixRecord.setAppendixUrl(appendixDto.getAppendixUrl());
                caseAppendixRecord.setAppendixName(appendixDto.getAppendixName());
                caseAppendixRecord.setCaseStageName("");
                caseAppendixRecord.setDocumentCatalogCode("");
                caseAppendixRecord.setCaseLinkCode("");
                caseAppendixRecord.setCaseLinkName("");
                caseAppendixRecord.setId(id);
                caseAppendixRecord.setUploadTime(LocalDateTime.now());
                caseAppendixRecord.setUploadUserId(userDTO.getUserId().toString());
                caseAppendixRecord.setUploadUserName(userDTO.getRealName());
                caseAppendixRecord.setIsDelete(Constant.STRING_0);
                caseAppendixRecord.setAppendixStatus(Constant.STRING_1);
                caseAppendixRecordMapper.insert(caseAppendixRecord);
                if(StringUtils.isBlank(attachmentId)){
                    attachmentId.append(id);
                }else{
                    attachmentId.append(","+id);
                }
            }
        }
        return attachmentId.toString();
    }

    /**
     * 修改机动车违停记录
     * @param dto
     * @return
     */
    @Override
    public Integer updateIllegalParkingRecord(CaseIllegalParkingRecordDto dto) {
        CaseIllegalParkingRecord caseIllegalParkingRecord = new CaseIllegalParkingRecord();
        BeanUtils.copyProperties(dto, caseIllegalParkingRecord);
        AuditDataUtil.fillAuditDataForUpdate(caseIllegalParkingRecord);
        return caseIllegalParkingRecordMapper.updateByPrimaryKeySelective(caseIllegalParkingRecord);
    }

    /**
     * 删除机动车违停记录
     * @param dto
     * @return
     */
    @Override
    public Integer deleteIllegalParkingRecord(CaseIllegalParkingRecordDto dto) {
        CaseIllegalParkingRecord caseIllegalParkingRecord = new CaseIllegalParkingRecord();
        caseIllegalParkingRecord.setIsDeleted("1");
        Example example = new Example(CaseIllegalParkingRecord.class);
        example.createCriteria().andEqualTo("id", dto.getId());
        return caseIllegalParkingRecordMapper.updateByExampleSelective(caseIllegalParkingRecord, example);
    }

    /**
     * 获取机动车违停记录详情
     * @param dto
     * @return
     */
    @Override
    public CaseIllegalParkingRecordVo getIllegalParkingRecordDetail(CaseIllegalParkingRecordDto dto) {
        Example example = new Example(CaseAppendixRecord.class);
        example.createCriteria().andEqualTo("caseId",dto.getId()).andEqualTo("isDelete","0");
        List<CaseAppendixRecord> appendixRecords = caseAppendixRecordMapper.selectByExample(example);
        CaseIllegalParkingRecordVo illegalParkingRecordDetail = caseIllegalParkingRecordMapper.getIllegalParkingRecordDetail(dto);
        illegalParkingRecordDetail.setAppendixList(appendixRecords);
        return illegalParkingRecordDetail;
    }


}